#include "stdafx.h"
#include "render/rendermgr.h"
#include "./readyhelper.h"

#define WAIT_TIME 0.5
#define ANGLE_SPEED 300
#define TEXT_COLOR Color(200, 200, 00)

ReadyHelper::ReadyHelper(Size stageSize)
: m_ended(false)
, m_angle(90)
, m_status(0)
, m_waitTime(0)
, m_textOne("1", Point(stageSize.cx / 3. + 10, stageSize.cy / 3. + 5), stageSize.cy / 2., stageSize.cx / 3., TEXT_COLOR)
, m_textTwo("2", Point(stageSize.cx / 3. + 10, stageSize.cy / 3. + 5), stageSize.cy / 2., stageSize.cx / 3., TEXT_COLOR)
, m_textThree("3", Point(stageSize.cx / 3. + 10, stageSize.cy / 3. + 5), stageSize.cy / 2., stageSize.cx / 3., TEXT_COLOR)
, m_textGo("GO", Point(stageSize.cx / 4. + 10, stageSize.cy / 3. + 5), stageSize.cy / 2., stageSize.cx / 3., TEXT_COLOR)
{
	m_textOne.SetVisible(false);
	m_textTwo.SetVisible(false);
	m_textThree.SetVisible(false);
	m_textGo.SetVisible(false);

	RenderMgr::GetInstance().OnBeforeRender += EventObject<OnBeforeRenderFunc>(this, &ReadyHelper::OnBeforeRender);
}

ReadyHelper::~ReadyHelper()
{
	RenderMgr::GetInstance().OnBeforeRender -= EventObject<OnBeforeRenderFunc>(this, &ReadyHelper::OnBeforeRender);
}

void ReadyHelper::OnBeforeRender(float differTime)
{
	if (m_ended)
		return;

	Text3D* pText = NULL;

	switch (m_status)
	{
	case 0:
		pText = &m_textThree;
		break;
	case 1:
		pText = &m_textTwo;
		break;
	case 2:
		pText = &m_textOne;
		break;
	case 3:
		pText = &m_textGo;
		break;
	}

	pText->SetVisible(true);

	if (m_waitTime > 0)
	{
		m_waitTime -= differTime;

		if (m_waitTime <= 0)
		{
			m_angle = 90;
			m_status++;
			if (m_status > 3)
			{
				m_ended = true;
				pText->SetVisible(false);
				FireEvent(OnReadyEndFunc, OnEnd, ())
			}
			pText->SetVisible(false);
		}

		return;
	}
	
	m_angle -= differTime * ANGLE_SPEED;

	pText->Rotate(m_angle);

	if (m_angle <= 0)
	{
		m_angle = 0;
		m_waitTime = WAIT_TIME;
	}
}